﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _03.LongestSequenceOfStringsInMatrix
{
    class Program
    {
        static void Main(string[] args)
        {
            //q = number of rows
            //p = number of columns
            string[,] matrix = {{"trf", "k"  , "m"  ,"m"   },
                               { "trf", "m"  , "m"  ,"trf" },
                               { "trf", "m"  , "trf","jab" },
                               { "m"  , "trf", "k"  ,"ktl" },
                               { "asd", "m"  , "no" ,"psi"}};
            int count = 0, bestCount = 0;
            string current = "", best = "";
            int i, k, n = 0, m = 0;
            for (i = 0; i < matrix.GetLength(0); i++)
            {
                count = 1;
                for (k = 0; k < matrix.GetLength(1) - 1; k++)
                {
                    if (matrix[i, k + 1] == matrix[i, k])
                    {
                        count++;
                        current = matrix[i, k];
                    }
                }
                if (count >= bestCount)
                {
                    bestCount = count;
                    best = current;
                }
            }
            for (k = 0; k < matrix.GetLength(1); k++)
            {
                count = 1;
                for (i = 0; i < matrix.GetLength(0) - 1; i++)
                {
                    if (matrix[i + 1, k] == matrix[i, k])
                    {
                        count++;
                        current = matrix[i, k];
                    }
                }
                if (count >= bestCount)
                {
                    bestCount = count;
                    best = current;
                }
            }
            for (i = 0; i < matrix.GetLength(0); i++)
            {
                for (k = 0; k < matrix.GetLength(1); k++)
                {
                    count = 1;
                    m = i;
                    n = k;
                    while (m != matrix.GetLength(0) - 1 && n != matrix.GetLength(1) - 1)
                    {
                        if (matrix[m + 1, n + 1] == matrix[m, n])
                        {
                            count++;
                            current = matrix[m, n];
                        }
                        m++;
                        n++;
                    }
                    if (count >= bestCount)
                    {
                        bestCount = count;
                        best = current;
                    }
                }
            }
            for (i = 0; i < matrix.GetLength(0); i++)
            {
                for (k = matrix.GetLength(1) - 1; k >= 0; k--)
                {
                    count = 1;
                    m = i;
                    n = k;
                    while (m != matrix.GetLength(0) - 1 && n != 0)
                    {
                        if (matrix[m + 1, n - 1] == matrix[m, n])
                        {
                            count++;
                            current = matrix[m, n];
                        }
                        m++;
                        n--;
                    }
                    if (count >= bestCount)
                    {
                        bestCount = count;
                        best = current;
                    }
                }
            }
            for (i = 0; i < bestCount - 1; i++)
            {
                Console.Write("{0}, ", best);
            }
            Console.WriteLine(best);
        }
    }
}